<HTML>
<HEAD>
<TITLE>
DynamicLibrary.h
</TITLE>
</HEAD>
<BODY>
<PRE>
<font color="green">/*
* Copyright 2006 the original author or authors.
* 
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* 
*      http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/</font>

<font color="blue">#ifndef</font> AUTUMN_DYNAMICLIBRARY_H
<font color="blue">#define</font> AUTUMN_DYNAMICLIBRARY_H

<font color="blue">#include</font> <font color="maroon">&#60;string&#62;</font>
<font color="blue">#include</font> <font color="maroon">"Basic.h"</font>
<font color="blue">#include</font> <font color="maroon">"AutumnException.h"</font>
<font color="blue">#include</font> <font color="maroon">"ILibrary.h"</font>

<font color="blue">#ifdef</font> WIN32
    <font color="blue">#include</font> <font color="maroon">&#60;windows.h&#62;</font>

    <font color="blue">#define</font> _OPENDLL_<font color="black">(</font>libname<font color="black">)</font> LoadLibrary<font color="black">(</font>libname<font color="black">)</font>
    <font color="blue">#define</font> _GETFUNCTION_<font color="black">(</font>libpoint,funname<font color="black">)</font> GetProcAddress<font color="black">(</font>libpoint, funname<font color="black">)</font>
    <font color="blue">#define</font> _CLOSEDLL_<font color="black">(</font>libpoint<font color="black">)</font> FreeLibrary<font color="black">(</font>libpoint<font color="black">)</font>
    <font color="blue">#define</font> _DLLHANDLE_ HINSTANCE
<font color="blue">#else</font>
    <font color="blue">#include</font> <font color="maroon">&#60;dlfcn.h&#62;</font>

    <font color="blue">#define</font> _OPENDLL_<font color="black">(</font>libname<font color="black">)</font> dlopen<font color="black">(</font>libname, RTLD_LAZY<font color="black">)</font>
    <font color="blue">#define</font> _GETFUNCTION_<font color="black">(</font>libpoint,funname<font color="black">)</font> dlsym<font color="black">(</font>libpoint, funname<font color="black">)</font>
    <font color="blue">#define</font> _CLOSEDLL_<font color="black">(</font>libpoint<font color="black">)</font> dlclose<font color="black">(</font>libpoint<font color="black">)</font>
    <font color="blue">#define</font> _DLLHANDLE_ <font color="blue">void</font><font color="black">*</font>
<font color="blue">#endif</font>


<font color="blue">using</font> <font color="blue">namespace</font> std;

<font color="blue">namespace</font> Autumn<font color="black">{</font>

<font color="green">/** 
 * Dynamic library 
 * @version 0.1.0
 * @since 2006-12-16
 */</font>

<font color="blue">class</font> DynamicLibrary<font color="black">:</font> <font color="blue">public</font> ILibrary<font color="black">{</font>
<font color="blue">private</font><font color="black">:</font>
    string Name;
    string Path;
    _DLLHANDLE_ Handle;

<font color="blue">public</font><font color="black">:</font>
    <font color="green">/** Constructor */</font>
    DynamicLibrary<font color="black">(</font><font color="blue">const</font> string<font color="black">&</font> <font color="blue">name</font>, <font color="blue">const</font> string<font color="black">&</font> path<font color="black">)</font>
                   <font color="black">:</font>Name<font color="black">(</font><font color="blue">name</font><font color="black">)</font>, Path<font color="black">(</font>path<font color="black">)</font><font color="black">{</font>
        <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>Handle <font color="black">=</font> _OPENDLL_<font color="black">(</font>path.c_str<font color="black">(</font><font color="black">)</font><font color="black">)</font>;
        <font color="blue">if</font><font color="black">(</font> <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>Handle <font color="black">=</font><font color="black">=</font> NULL<font color="black">)</font><font color="black">{</font>
            <font color="blue">throw</font> NotFoundEx<font color="black">(</font><font color="maroon">"DynamicLibrary"</font>, <font color="maroon">"DynamicLibrary"</font>, 
                <font color="maroon">"Library ["</font> <font color="black">+</font> path <font color="black">+</font> <font color="maroon">"] not found!"</font><font color="black">)</font>;
        <font color="black">}</font>
    <font color="black">}</font>

    <font color="green">/** Destructor */</font>
    ~DynamicLibrary<font color="black">(</font><font color="black">)</font><font color="black">{</font>
        AutumnLog<font color="black">:</font><font color="black">:</font>getInstance<font color="black">(</font><font color="black">)</font><font color="black">-</font><font color="black">&#62;</font>debug<font color="black">(</font><font color="maroon">"DynamicLibrary-&#62;~DynamicLibrary"</font><font color="black">)</font>;
        <font color="blue">if</font><font color="black">(</font> <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>Handle <font color="black">!</font><font color="black">=</font> NULL <font color="black">)</font>
            _CLOSEDLL_<font color="black">(</font><font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>Handle<font color="black">)</font>;
    <font color="black">}</font>

    <font color="green">/** Get function address */</font>
    <font color="blue">void</font><font color="black">*</font> getFunction<font color="black">(</font><font color="blue">const</font> string<font color="black">&</font> <font color="blue">name</font><font color="black">)</font><font color="blue">const</font><font color="black">{</font>
        <font color="blue">void</font><font color="black">*</font> p <font color="black">=</font> _GETFUNCTION_<font color="black">(</font><font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>Handle, <font color="blue">name</font>.c_str<font color="black">(</font><font color="black">)</font><font color="black">)</font>;
        <font color="blue">if</font><font color="black">(</font> p <font color="black">)</font>
            <font color="blue">return</font> p;
        <font color="blue">throw</font> NotFoundEx<font color="black">(</font><font color="maroon">"DynamicLibrary"</font>, <font color="maroon">"getFunction"</font>, 
            <font color="maroon">"function ["</font> <font color="black">+</font> <font color="blue">name</font> <font color="black">+</font> <font color="maroon">"] not found!"</font><font color="black">)</font>;
    <font color="black">}</font>
<font color="black">}</font>;

<font color="black">}</font> <font color="green">// End namespace Autumn</font>
<font color="blue">#endif</font>

</PRE>
</BODY>
</HTML>
